package leetcode.editor.cn;

import 抽象数据类型.ListNode;

import java.util.Arrays;


public class MergeListNode {

    public static ListNode proList(int[] arr, int index, ListNode root) {

        while (index < arr.length) {
            ListNode temp = root;
            ListNode cur = new ListNode(arr[index]);
            if (root == null) {
                root = cur;
                index++;
                continue;
            }
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = cur;
            index++;


        }

        return root;

    }


    public static void prtList(ListNode root) {
        StringBuilder stringBuilder = new StringBuilder();

        if (root != null) {
            System.out.print(root.val + " ");
            prtList(root.next);
        }


    }

    public static ListNode MergeListNode(ListNode L1, ListNode L2) {
        ListNode listNode = new ListNode(1);
        if (L1 == null || L2 == null) {
            return L1 == null ? (L2 == null ? null : L2) : L1;
        }
        ListNode temp;
        ListNode cur = null;
        while (L1 != null || L2 != null) {
            temp = listNode;
            if (L1 != null && L2 != null) {
                if (L1.val > L2.val) {
                    cur = new ListNode(L2.val);
                    L2 = L2.next;
                } else {
                    cur = new ListNode(L1.val);
                    L1 = L1.next;
                }
            } else if (L1 != null) {
                cur = new ListNode(L1.val);
                L1 = L1.next;
            } else if (L2 != null) {
                cur = new ListNode(L2.val);
                L2 = L2.next;
            }
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = cur;
        }


        return listNode.next;
    }

    public static ListNode deleteRe(ListNode root) {
        ListNode temp = root;
        while (temp.next != null) {
            if (temp.val == temp.next.val) {
                if (temp.next.next != null) {
                    ListNode curNode = temp.next.next;
                    temp.next.next = null;
                    temp.next = curNode;
                } else {
                    temp.next = null;
                }


            } else {
                temp = temp.next;
            }
        }
        return root;
    }

    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int i = 0;
        int j = 0;
        if (m == 0) {
            for (int l = 0; l < n; l++) {
                nums1[l] = nums2[l];
            }
            return;
        }
        else if (n == 0) {

            return;
        }
      /*  if (m < n) {
            int[] temp = nums1;
            nums1 = nums2;
            nums2 = temp;
            m = m + n;
            n = m - n;
            m = m - n;
        }*/
        while (i < nums1.length || j < nums2.length) {
            if (j < n && i < m && nums1[i] >= nums2[j]) {
                for (int k = m; k > i; k--) {
                    nums1[k] = nums1[k - 1];
                }
                nums1[i] = nums2[j];
                m++;
                j++;
            }
            i++;


        }
        if (i >= m - 1) {
            for (int k = m; k < m + n - j; k++) {
                nums1[k] = nums2[j];
                j++;
            }
            return;
        }


    }

    public static void main(String[] args) {

        int[] arr1 = {4, 0, 0, 0, 0};
        int[] arr2 = {1, 2, 5, 6};

        //  prtList(MergeListNode(proList(arr1, 0, null), proList(arr2, 0, null)));
        //prtList(deleteRe(proList(arr1,0,null)));
        merge(arr1, 1, arr2, 4);
        System.out.println(Arrays.toString(arr1));
    }
}
